<?php

namespace Trois\Init\Inits;

use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use Trois\Design\Observer\IObserver;

class OrmInit implements IObserver
{
    use Singleton;

    public function update()
    {
        $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
        $hosts = explode(',', $config->getHost());

        foreach ($hosts as $k => $host) {
            $config->setHost($host);
            $config->setReturnCollection(true); //设置返回结果为结果集, 可以快速all()->toArray() 参考：http://www.easyswoole.com/Components/Orm/core.html
            DbManager::getInstance()->addConnection(new Connection($config), ($k == 0 ? 'default' : 'read' . $k));
        }

    }
}